package cn.afactory.tygz.controller;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import java.util.ArrayList;

import cn.afactory.tygz.cache.LocalJsonCache;
import cn.afactory.tygz.model.AdInfoList;
import cn.afactory.tygz.model.ApartmentInfoList;
import cn.afactory.tygz.model.EntertainmentInfoList;
import cn.afactory.tygz.model.LifeServiceInfoList;
import cn.afactory.tygz.model.RestaurantInfoList;
import cn.afactory.tygz.model.ScenicSpotsInfoList;
import cn.afactory.tygz.model.SecondGoodsInfoList;
import cn.afactory.tygz.model.ServiceTypeList;
import cn.afactory.tygz.model.SnsCommentInfoList;
import cn.afactory.tygz.model.SnsTopicInfoList;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.entity.BannerInfo;
import cn.afactory.tygz.ui.base.entity.BannerInfoList;

/**
 * Created by Kenneth on 15/9/8.
 */
public class CommunityController extends BaseLogicController {
    public static final int MSG_DATA_FROM_CACHE = -2000;

    public static final int MSG_GET_APARTMENT_LIST_OK = 2001;
    public static final int MSG_GET_APARTMENT_LIST_KO = 2002;
    public static final int MSG_GET_APARTMENT_DETAIL_OK = 2003;
    public static final int MSG_GET_APARTMENT_DETAIL_KO = 2004;

    public static final int MSG_GET_ENTERTAIN_LIST_OK = 2005;
    public static final int MSG_GET_ENTERTAIN_LIST_KO = 2006;
    public static final int MSG_GET_ENTERTAIN_DETAIL_OK = 2007;
    public static final int MSG_GET_ENTERTAIN_DETAIL_KO = 2008;

    public static final int MSG_GET_LIFE_SERVICE_LIST_OK = 2009;
    public static final int MSG_GET_LIFE_SERVICE_LIST_KO = 2010;
    public static final int MSG_GET_LIFE_SERVICE_DETAIL_OK = 2011;
    public static final int MSG_GET_LIFE_SERVICE_DETAIL_KO = 2012;

    public static final int MSG_GET_RESTAURANT_LIST_OK = 2013;
    public static final int MSG_GET_RESTAURANT_LIST_KO = 2014;
    public static final int MSG_GET_RESTAURANT_DETAIL_OK = 2015;
    public static final int MSG_GET_RESTAURANT_DETAIL_KO = 2016;

    public static final int MSG_GET_SECOND_GOOD_LIST_OK = 2017;
    public static final int MSG_GET_SECOND_GOOD_LIST_KO = 2018;
    public static final int MSG_GET_SECOND_GOOD_DETAIL_OK = 2019;
    public static final int MSG_GET_SECOND_GOOD_DETAIL_KO = 2020;

    public static final int MSG_GET_SCENIC_SPOT_LIST_OK = 2021;
    public static final int MSG_GET_SCENIC_SPOT_LIST_KO = 2022;
    public static final int MSG_GET_SCENIC_SPOT_DETAIL_OK = 2023;
    public static final int MSG_GET_SCENIC_SPOT_DETAIL_KO = 2024;

    public static final int MSG_POST_COMMUNITY_COMMENT_OK = 2025;
    public static final int MSG_POST_COMMUNITY_COMMENT_KO = 2026;
    public static final int MSG_GET_COMMUNITY_COMMENT_OK = 2027;
    public static final int MSG_GET_COMMUNITY_COMMENT_KO = 2028;

    public static final int MSG_GET_SERVICE_TYPE_OK = 0x3001;
    public static final int MSG_GET_SERVICE_TYPE_KO = 0x3002;
    //我发布的二手物品
    public static final int MSG_GET_MY_SECOND_GOOD_LIST_OK = 0x3004;
    public static final int MSG_GET_MY_SECOND_GOOD_LIST_KO = 0x3005;
    //广告信息
    public static final int MSG_GET_AD_LIST_OK = 0x3007;
    public static final int MSG_GET_AD_LIST_KO = 0x3008;

    public static final int MSG_LOAD_BANNER_DATA = 2000;
    //社区评论
    public static final int MSG_POST_COMM_COMMENT_OK = 0x4001;
    public static final int MSG_POST_COMM_COMMENT_KO = 0x4002;
    //社区评论列表
    public static final int MSG_GET_COMM_COMMENT_LIST_OK = 0x4003;
    public static final int MSG_GET_COMM_COMMENT_LIST_KO = 0x4004;
    //热门话题列表
    public static final int MSG_GET_HOT_TOPIC_LIST_OK = 0x4005;
    public static final int MSG_GET_HOT_TOPIC_LIST_KO = 0x4006;
    //推荐话题列表
    public static final int MSG_GET_RECOMM_TOPIC_LIST_OK = 0x4007;
    public static final int MSG_GET_RECOMM_TOPIC_LIST_KO = 0x4008;

    private CommunityCacheManager mCommunityCacheManager;
    private static String TAG;

    public CommunityController(Context context, Handler handler, String CacheTag) {
        super(context, handler);
        TAG = mContext.getClass().getSimpleName();
        mCommunityCacheManager = new CommunityCacheManager(!TextUtils.isEmpty(CacheTag) ? CacheTag : "");
    }

    /**
     * 获取bannner
     */
    public void loadBannerData() {
        //访问网络
//        NetManager.getInstance(mContext).doGetBannerInfos(BANNER_INQUIRY_HALL,
//                new OnGetBannerInfosListener() {
//
//                    @Override
//                    public void onInernError(int errorCode, String errorMessage) {
//                        sendMessage(MSG_LOAD_BANNER_DATA, errorCode, 0, errorMessage);
//                    }
//
//                    @Override
//                    public void onComplete(boolean isOK,
//                                           BannerInfoList bannerInfoList, int errorCode,
//                                           String errorMsg) {
//                        if (isOK) {
//                            if (bannerInfoList != null
//                                    && bannerInfoList.value.size() > 0) {
//                                sendMessage(MSG_LOAD_BANNER_DATA, bannerInfoList);
//                                mHallCacheManager.brannerCache.save(BannerInfoList.class.getSimpleName()
//                                        + BANNER_INQUIRY_HALL, bannerInfoList);
//                                return;
//                            }
//                        }
//                        sendMessage(MSG_LOAD_BANNER_DATA, errorCode, 0, null);
//                    }
//                }
//        );
        // for mock data
//        BannerInfoList bannerInfoList = new BannerInfoList();
//        BannerInfo bannerInfo = new BannerInfo();
//        bannerInfo.imgUrl = "";
//        bannerInfoList.value = new ArrayList<BannerInfo>();
//        bannerInfoList.value.add(bannerInfo);
//        mCommunityCacheManager.nBannerInfoListCache.save(BannerInfoList.class.getSimpleName(),bannerInfoList);
//        sendMessage(MSG_LOAD_BANNER_DATA, bannerInfoList);
    }

    /**
     * (六类评论  类型 1代表二手物品 2代表餐饮 3代表房间 4代表娱乐场所 5代表周边景点 6代表生活服务）
     * @param postId
     * @param content
     * @param type
     */
    public void doPostCommunityComment(final long postId, final String content, final int type) {
        NetManager.getInstance(mContext).doPostCommComment(postId, content, type, new OnResponseListener<Boolean>() {
            @Override
            public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_POST_COMMUNITY_COMMENT_OK, result);
                    return;
                }
                sendMessage(MSG_POST_COMMUNITY_COMMENT_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_POST_COMMUNITY_COMMENT_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取人才公寓列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadApartmentInfoList(final double lat,
                                      final double lng,
                                      final int mPageIndex,
                                      final int mPageSize) {
        NetManager.getInstance(mContext).doGetRoomList(lat, lng, mPageIndex, mPageSize, new OnResponseListener<ApartmentInfoList>() {
            @Override
            public void onComplete(boolean isOK, ApartmentInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_APARTMENT_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_APARTMENT_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_APARTMENT_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * (六类评论  类型 1代表二手物品 2代表餐饮 3代表房间 4代表娱乐场所 5代表周边景点 6代表生活服务）
     * @param postId
     * @param page
     * @param row
     * @param type
     */
    public void loadCommunityCommentList(final long postId ,
                                         final int page ,
                                         final int row,
                                         final int type) {
        NetManager.getInstance(mContext).doGetCommCommentList(postId, page, row, type, new OnResponseListener<SnsCommentInfoList>() {
            @Override
            public void onComplete(boolean isOK, SnsCommentInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_COMMUNITY_COMMENT_OK, result);
                    return;
                }
                sendMessage(MSG_GET_COMMUNITY_COMMENT_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_COMMUNITY_COMMENT_KO, errorCode, 0, errorMessage);
            }
        });
    }


    /**
     * 获取娱乐场所列表
     * @param lat
     * @param lng
     * @param typeId
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadEntertainList(final double lat, final double lng, final int typeId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetFuckList(lat, lng, typeId, mPageIndex, mPageSize, new OnResponseListener<EntertainmentInfoList>() {
            @Override
            public void onComplete(boolean isOK, EntertainmentInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_ENTERTAIN_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_ENTERTAIN_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_ENTERTAIN_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取人才公寓详情
     * @param entertainId
     */
    public void loadEntertainDetail(final long entertainId) {

    }

    /**
     *获取生活服务列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadLifeServiceList(final double lat, final double lng, final int typeId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetServiceList(lat, lng, typeId, mPageIndex, mPageSize, new OnResponseListener<LifeServiceInfoList>() {
            @Override
            public void onComplete(boolean isOK, LifeServiceInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_LIFE_SERVICE_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_LIFE_SERVICE_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_LIFE_SERVICE_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获生活服务详情
     * @param lifeServiceId
     */
    public void loadLifeServiceDetail(final long lifeServiceId) {

    }

    /**
     *获取餐饮列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadRestaurantList(final double lat, final double lng, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetFoodsList(lat, lng, mPageIndex, mPageSize, new OnResponseListener<RestaurantInfoList>() {
            @Override
            public void onComplete(boolean isOK, RestaurantInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_RESTAURANT_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_RESTAURANT_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_RESTAURANT_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获餐饮详情
     * @param restaurantId
     */
    public void loadRestaurantDetail(final long restaurantId) {

    }

    /**
     *获取二手物品列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadSecondGoodsList(final double lat, final double lng,final int typeId, final int mPageIndex, final int mPageSize, final int sort) {
        NetManager.getInstance(mContext).doGetSecondsList(lat, lng, typeId, mPageIndex, mPageSize, sort, new OnResponseListener<SecondGoodsInfoList>() {
            @Override
            public void onComplete(boolean isOK, SecondGoodsInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_SECOND_GOOD_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_SECOND_GOOD_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_SECOND_GOOD_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获二手物品详情
     * @param secondGoodId
     */
    public void loadSecondGoodDetail(final long secondGoodId) {

    }

    /**
     *获取周边景点列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void loadScenicSpotsList(final double lat, final double lng, final int typeId, final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetSpotList(lat, lng, typeId, mPageIndex, mPageSize, new OnResponseListener<ScenicSpotsInfoList>() {
            @Override
            public void onComplete(boolean isOK, ScenicSpotsInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_SCENIC_SPOT_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_SCENIC_SPOT_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_SCENIC_SPOT_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获周边景点详情
     * @param scenicSpotId
     */
    public void loadScenicSpotDetail(final long scenicSpotId) {

    }

    /**
     * 加载服务类型列表
     * @param type (0 表示 娱乐场所类型  1 表示生活服务类型  2 表示 二手物品类型)
     */
    public void loadServiceTypeList(int type) {
        NetManager.getInstance(mContext).doGetServiceTypeList(type, 1, 1000, new OnResponseListener<ServiceTypeList>() {
            @Override
            public void onComplete(boolean isOK, ServiceTypeList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_SERVICE_TYPE_OK, result);
                    return;
                }
                sendMessage(MSG_GET_SERVICE_TYPE_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_SERVICE_TYPE_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取我发布的二手物品列表
     */
    public void loadMyPublishSecondGoodsList(long userId,int pageIndex,int pageSize) {
        NetManager.getInstance(mContext).doGetMyPublishSecondGoodsList(userId, pageIndex, pageSize, new OnResponseListener<SecondGoodsInfoList>() {
            @Override
            public void onComplete(boolean isOK, SecondGoodsInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_MY_SECOND_GOOD_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_MY_SECOND_GOOD_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_MY_SECOND_GOOD_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取社区广告信息
     */
    public void doLoadCommAds() {
        NetManager.getInstance(mContext).doGetAd(1, new OnResponseListener<AdInfoList>() {
            @Override
            public void onComplete(boolean isOK, AdInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_AD_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_AD_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_AD_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    /**
     * 获取启动页广告
     */
    public void doLoadSplashAds() {
        NetManager.getInstance(mContext).doGetAd(0, new OnResponseListener<AdInfoList>() {
            @Override
            public void onComplete(boolean isOK, AdInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    sendMessage(MSG_GET_AD_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_GET_AD_LIST_KO, errorCode, 0, errorMsg);
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_GET_AD_LIST_KO, errorCode, 0, errorMessage);
            }
        });
    }

    public void loadDataFromCache() {
        mCommunityCacheManager.loadFromCache();
    }

    /**
     * 数所缓存管理
     */
    private class CommunityCacheManager {

        public static final String HEALTH_CIRCLE_CACHE = "COMMUNITY_CACHE";
        private String key_subjects;
        LocalJsonCache<BannerInfoList> nBannerInfoListCache;
        private CommunityCacheManager(String cachetag) {
            key_subjects = TAG + HEALTH_CIRCLE_CACHE + cachetag;

            nBannerInfoListCache = new LocalJsonCache<BannerInfoList>(mContext);
            nBannerInfoListCache.setCallback(new LocalJsonCache.Callback<BannerInfoList>() {
                @Override
                public void onSave(String path) {
                }

                @Override
                public void onParse(String path, BannerInfoList t) {
                    if (t != null) {
                        sendMessage(MSG_LOAD_BANNER_DATA, MSG_DATA_FROM_CACHE, 0, t);
                    }
                }
            });
        }

        public void saveBannerInfoList(BannerInfoList t) {
            nBannerInfoListCache.save(key_subjects, t);
        }

        public void loadFromCache() {
            nBannerInfoListCache.load(key_subjects, BannerInfoList.class);
        }
    }

    /**
     * 获取推荐话题列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetRecommTopicList(final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetTopicList(
                2,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsTopicInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_GET_RECOMM_TOPIC_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsTopicInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsTopicInfoList();
                            }
                            sendMessage(MSG_GET_RECOMM_TOPIC_LIST_OK, result);
                            return;
                        }
                        sendMessage(MSG_GET_RECOMM_TOPIC_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

    /**
     * 获取热门话题列表
     * @param mPageIndex
     * @param mPageSize
     */
    public void doGetHotTopicList(final int mPageIndex, final int mPageSize) {
        NetManager.getInstance(mContext).doGetTopicList(
                1,
                mPageIndex,
                mPageSize,
                new OnResponseListener<SnsTopicInfoList>() {

                    @Override
                    public void onInternError(int errorCode, String errorMessage) {
                        sendMessage(MSG_GET_HOT_TOPIC_LIST_KO, errorCode, 0, errorMessage);
                    }

                    @Override
                    public void onComplete(boolean isOK, SnsTopicInfoList result, int errorCode, String errorMsg) {
                        if (isOK) {
                            if (result == null) {
                                result = new SnsTopicInfoList();
                            }
                            sendMessage(MSG_GET_HOT_TOPIC_LIST_OK, result);
                            return;
                        }
                        sendMessage(MSG_GET_HOT_TOPIC_LIST_KO, errorCode, 0, errorMsg);
                    }
                }
        );
    }

}
